#!/bin/bash

ARG=$1

function logmsg {
    /usr/bin/logger -p user.info udevumnt $ARG: $@
}

if [ -z $1 ] ; then
    logmsg "no device supplied. Exiting."
    exit 1
fi

DEVICESHORT=$1
DEVICE=/dev/$1

MOUNTPOINT=`cat /media/.udevmnt/$DEVICESHORT`
if [ "x$MOUNTPOINT" = "x$i" ]; then
    logmsg "no mountpoint found for $DEVICESHORT. Exiting."
    exit 1
fi

/bin/umount -l $MOUNTPOINT

/bin/rm /media/.udevmnt/$DEVICESHORT

# Test if mountpoint has something mounted there
for i in `cat /proc/mounts | cut -d' ' -f1`; do
    if [ "x$MOUNTPOINT" = "x$i" ]; then
        logmsg "something still mounted at $MOUNTPOINT, i won''t delete it"
        exit 1
    fi
done

/bin/rmdir $MOUNTPOINT
